<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-10-05 14:55:13
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\Storage;


use App\Http\Services\Service;
use App\Repositories\MpcReelStructRepository;

/**
 * @property MpcReelStructRepository $repository  
 */
class ReelStructService extends Service
    {
    public function boot(MpcReelStructRepository $repository)
        {
        $this->repository = $repository;
        }

    public function list()
        {
        $data = $this->repository->condition(request())->getGrid();
        return $data;
        }
    public function info()
        {
        return $this->repository->condition(request())->setRelation(["struct_new_storage" => ['customer', 'material', 'unit'], "struct_storage" => ['customer', 'material', 'unit', 'location']])->getOne();
        }
    protected function save()
        {
        $request = request()->toArray();
        $gdata   = $this->repository->autosave($request, false, ['struct_new_storage', 'struct_storage']);
        $gdata->struct_new_storage->filter(function ($v) use ($gdata)
            {
            $v->batchno = $gdata->code;
            $v->save();
            });
        return $gdata;
        }

    protected function audit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 0) {
                fail("{$v->code}单据状态{$v->status->name},操作失败！");
                }
            $v->struct_storage->filter(function ($vv) use ($v)
                {
                $vv->setWhere([
                    'material_id' => ['key' => 'material_id'],
                    'dyelot'      => ['key' => 'dyelot', 'default' => ''],
                    'grade'       => ['key' => 'grade', 'default' => 'A'],
                    'location_id' => ['key' => 'location_id'],
                    'customer_id' => ['key' => 'customer_id', 'default' => 0],
                    'iscustomer'  => ['key' => 'iscustomer', 'default' => 0],
                    'batchno'     => ['key' => 'batchno'],
                    'reel'        => ['key' => 'reel', 'default' => 0]
                ])->useStorage('分合卷出库', $v->code)->subStorage();
                });
            $v->struct_new_storage->filter(function ($vv) use ($v)
                {
                $vv->addStorage('分合卷入库', $v->code);
                });
            auditSave($v);
            });
        }

    protected function noaudit()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 1) {
                fail("{$v->code}单据状态{$v->status->name},操作失败！");
                }
            $v->struct_storage->filter(function ($vv) use ($v)
                {
                $vv->nosubStorage()->nouseStorage();
                });
            $v->struct_new_storage->filter(function ($vv) use ($v)
                {
                $vv->noaddStorage();
                });
            auditSave($v, false);
            });
        }

    protected function delete()
        {
        return $this->repository->whereIn("id", $this->getRequestIds())->get()->filter(function ($v)
            {
            if ($v->status_id != 0) {
                fail("{$v->code}单据状态{$v->status->name},操作失败！");
                }
            $v->struct_new_storage()->delete();
            $v->struct_storage()->delete();
            $v->delete();
            });
        }
    }